const { Comment } = require('../model/comment')
const { Article } = require('../model/article')

const add = async ctx => {
    try {
        let comment = ctx.request.body
        let isComment = false
        const rel = await Comment.create(comment)
        if(rel) {
            isComment = true
            ctx.body = {
                code: 200,
                msg: '评论成功'
            }
        } else {
            ctx.body = {
                code: 400,
                msg: '评论失败'
            }

        }
        if(isComment) {
            await Article.updateOne({id: comment.articleId}, {$inc: {comment: 1}})
        }
    }
    catch (error) {
        ctx.body = {
            code: 500,
            msg: error
        }
    }
}

module.exports = {
    add
}